package cn.hfuu.controller;

import cn.hfuu.domain.Customer;
import cn.hfuu.domain.LinkMan;
import cn.hfuu.service.LinkManService;
import cn.hfuu.utils.PageBean;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

public class LinkManAction extends ActionSupport implements ModelDriven<LinkMan>{

    private LinkMan linkMan = new LinkMan();
    private LinkManService linkManService;
    private Integer currentPage;
    private Integer pageSize;

    public String add() throws Exception {
        //调用service层方法保存linkman对象
        linkManService.save(linkMan);
        //重定向到LinkMan list列表
        return "ToList";
    }
    public String list() throws Exception {
        //封装离线查询对象
        DetachedCriteria dc = DetachedCriteria.forClass(LinkMan.class);
        if(StringUtils.isNotBlank(linkMan.getLkm_name())) {
            dc.add(Restrictions.like("lkm_name", "%" + linkMan.getLkm_name() + "%"));
        }
        //调用service方法进行分页查询
        PageBean pb = linkManService.getPageBean(dc,currentPage,pageSize);
        ActionContext.getContext().put("pageBean",pb);
        return "list";
    }
    public String toEdit() throws Exception {
        //调用service层方法根据id查询
        System.out.println(linkMan.getLkm_id().toString());
        LinkMan lkm  = linkManService.getById(linkMan.getLkm_id());


        //保存到request域
        ActionContext.getContext().put("linkMan",lkm);

        return "toEdit";
    }

    public LinkMan getModel() {
        return linkMan;
    }

    public LinkManService getLinkManService() {
        return linkManService;
    }

    public void setLinkManService(LinkManService linkManService) {
        this.linkManService = linkManService;
    }

    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }
}
